package cn.chinaunicom.sdsi.frm.workflow.deployment.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chinaunicom.sdsi.frm.base.BaseController;
import cn.chinaunicom.sdsi.frm.workflow.deployment.controller.bean.ProcessDefinitionVO;
import cn.chinaunicom.sdsi.frm.workflow.deployment.service.ProcessDefinitionService;
import net.sf.json.JSONObject;

/**
 * 流程发布控制器
 * 
 * @author liyh
 * 
 */
@Controller("processDeploymentController")
@RequestMapping("processDeploymentController")
public class ProcessDeploymentController extends BaseController {

	private static final Log log = LogFactory
			.getLog(ProcessDeploymentController.class);

	/**
	 * 流程定义的查询组件
	 */
	private ProcessDefinitionService processDefinitionService;

	/**
	 * 系统内流程发布
	 * 
	 * @return
	 */
	@RequestMapping("/processDeployment")
	public String processDeployment() {

		log.debug("ProcessDeploymentController.processDeployment{}");
		return getToViewPage("workflow/processDeployment/processDeployment");
	}

	/**
	 * 翻页查找流程定义 前台使用jquery-datatable
	 * 
	 * @param vo
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/search4Page")
	public JSONObject search4Page(ProcessDefinitionVO vo, int start, int length) {

		log.debug("ProcessDeploymentController.search4Page{}:start:" + start
				+ ";length:" + length + ";vo" + vo);

		return this.getProcessDefinitionService().list4Page(vo, start, length);
	}

	/**
	 * 根据实体对象中取到的名称判断是否为新流程
	 * 
	 * @param vo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/adjustIsNewProcess")
	public JSONObject adjustIsNewProcess(ProcessDefinitionVO vo) {

		log.debug("ProcessDeploymentController.adjustIsNewProcess{}:vo.getKey:"
				+ vo.getKey());

		return this.getProcessDefinitionService().isNewProcess(vo);
	}

	/**
	 * 根据传参中的key部署新的流程
	 * 
	 * @param vo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deployProcess")
	public JSONObject deployProcess(ProcessDefinitionVO vo) {

		log.debug("ProcessDeploymentController.deployProcess{}:vo.getKey:"
				+ vo.getKey());

		return this.getProcessDefinitionService().addProcessByResource(vo);
	}

	/**
	 * 返回自定义的流程定义组件
	 * 
	 * @return
	 */
	public ProcessDefinitionService getProcessDefinitionService() {
		return processDefinitionService;
	}

	/**
	 * 使用@Autowired自动注入
	 * 
	 * @param processDefinitionService
	 */
	@Autowired
	public void setProcessDefinitionService(
			ProcessDefinitionService processDefinitionService) {
		this.processDefinitionService = processDefinitionService;
	}
}
